Completed
Push — some-scrutinizing ( e5bac5...edec13 )
by Maxence
02:26
created

$(document).ready   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 42
rs 8.5806
cc 4
nc 4
nop 1
1
/*
2
 * Circles - Bring cloud-users closer together.
3
 *
4
 * This file is licensed under the Affero General Public License version 3 or
5
 * later. See the COPYING file.
6
 *
7
 * @author Maxence Lange <[email protected]>
8
 * @copyright 2017
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25
26
/** global: OC */
27
/** global: OCA */
28
/** global: Notyf */
29
30
$(document).ready(function () {
31
32
	/**
33
	 * @constructs Navigation
34
	 */
35
	var Navigation = function () {
36
		this.initialize();
37
	};
38
39
	Navigation.prototype = {
40
41
		initialize: function () {
42
			var self = this;
43
			var api = OCA.Circles.api;
44
45
			var currCirclesType = '';
46
			var currentCircle = 0;
47
			var currentCircleLevel = 0;
48
			var lastSearchCircle = '';
49
			var lastSearchUser = '';
50
51
			var divNewTypeDefinition = $('#circles_new_type_definition');
52
			var divNewType = $('#circles_new_type');
53
			var divNewSubmit = $('#circles_new_submit');
54
			var divNewName = $('#circles_new_name');
55
			var divNavigation = $('#app-navigation.circles');
56
57
			divNewTypeDefinition.children('div').fadeOut(0);
58
			$('#circles_new_type_' + divNewType.children('option:selected').val()).fadeIn(0);
59
60
			divNewType.hide();
61
			divNewSubmit.hide();
62
			divNewTypeDefinition.hide();
63
64
			divNewName.on('keyup', function () {
65
				currentCircle = 0;
66
				currentCircleLevel = 0;
67
68
				divNavigation.hide('slide', 800);
69
				$('#circles_list div').removeClass('selected');
70
				$('#emptycontent').show(800);
71
				$('#mainui').fadeOut(800);
72
73
				if (divNewName.val() !== '') {
74
					divNewType.fadeIn(300);
75
					divNewSubmit.fadeIn(500);
76
					divNewTypeDefinition.fadeIn(700);
77
				}
78
				else {
79
					divNewType.fadeOut(700);
80
					divNewSubmit.fadeOut(500);
81
					divNewTypeDefinition.fadeOut(300);
82
				}
83
			});
84
85
			divNewType.on('change', function () {
86
87
				currentCircle = 0;
88
				currentCircleLevel = 0;
89
90
				divNavigation.hide('slide', 800);
91
				$('#circles_list div').removeClass('selected');
92
				$('#emptycontent').show(800);
93
				$('#mainui').fadeOut(800);
94
95
				divNewTypeDefinition.children('div').fadeOut(300);
96
				$('#circles_new_type_' + divNewType.children('option:selected').val()).fadeIn(
97
					300);
98
			});
99
100
			divNewSubmit.on('click', function () {
101
				api.createCircle(divNewType.val(), divNewName.val(),
102
					self.createCircleResult);
103
			});
104
105
			$('#circles_list div').on('click', function () {
106
				self.displayCirclesList($(this).attr('circle-type'));
107
			});
108
109
			$('#circles_search').on('input propertychange paste focus', function () {
110
				if (lastSearchCircle == $(this).val().trim()) {
111
					return;
112
				}
113
114
				lastSearchCircle = $(this).val().trim();
115
				api.searchCircles(currCirclesType, $(this).val().trim(),
116
					self.listCirclesResult);
117
			});
118
119
			$('.icon-circles').css('background-image',
120
				'url(' + OC.imagePath('circles', 'colored') + ')');
121
122
			$('#joincircle').on('click', function () {
123
				api.joinCircle(currentCircle, self.joinCircleResult);
124
			});
125
126
			$('#leavecircle').on('click', function () {
127
				api.leaveCircle(currentCircle, self.leaveCircleResult);
128
			});
129
130
			$('#joincircle_acceptinvit').on('click', function () {
131
				api.joinCircle(currentCircle, self.joinCircleResult);
132
			});
133
134
			$('#joincircle_rejectinvit').on('click', function () {
135
				api.leaveCircle(currentCircle, self.leaveCircleResult);
136
			});
137
138
			$('#addmember').on('input propertychange paste focus', function () {
139
140
				if (lastSearchUser == $(this).val().trim()) {
141
					return;
142
				}
143
144
				lastSearchUser = $(this).val().trim();
145
146
				$.get(OC.linkToOCS('apps/files_sharing/api/v1') + 'sharees',
147
					{
148
						format: 'json',
149
						search: $(this).val().trim(),
150
						perPage: 200,
151
						itemType: 'principals'
152
					}, self.searchMembersResult);
153
			}).blur(function () {
154
				$('#members_search_result').fadeOut(400);
155
			});
156
157
			$('#members_search_result').hide();
158
159
160
			this.createCircleResult = function (result) {
161
				var str = 'Circle';
162
				switch (result.type) {
163
					case '1':
164
						str = 'Personal circle';
165
						break;
166
					case '2':
167
						str = 'Hidden circle';
168
						break;
169
					case '4':
170
						str = 'Private circle';
171
						break;
172
					case '8':
173
						str = 'Public circle';
174
						break;
175
					default:
176
						break;
177
				}
178
179
				if (result.status == 1) {
180
					OCA.notification.onSuccess(str + " '" + result.name + "' created");
181
					self.displayCirclesList(result.circle.type);
182
					self.selectCircle(result.circle.id);
183
				}
184
				else {
185
					OCA.notification.onFail(
186
						str + " '" + result.name + "' NOT created: " +
187
						((result.error) ? result.error : 'no error message'));
188
				}
189
			};
190
191
192
			//
193
			//
194
			// Circles List
195
			this.displayCirclesList = function (type) {
196
197
				currCirclesType = type;
198
				lastSearchCircle = '';
199
				lastSearchUser = '';
200
201
				currentCircle = 0;
202
				currentCircleLevel = 0;
203
204
				divNavigation.show('slide', 800);
205
				$('#emptycontent').show(800);
206
				$('#mainui').fadeOut(800);
207
208
				$('#circles_search').val('');
209
				$('#addmember').val('');
210
211
				divNavigation.addClass('selected');
212
				$('#circles_list div').removeClass('selected');
213
214
				$('#circles_list').children().each(function () {
215
					if ($(this).attr('circle-type') == type.toLowerCase()) {
216
						$(this).addClass('selected');
217
					}
218
				});
219
220
				divNavigation.children().each(function () {
221
					if ($(this).attr('id') != 'circles_search') {
222
						$(this).remove();
223
					}
224
				});
225
				api.listCircles(type, self.listCirclesResult);
226
			};
227
228
229
			this.listCirclesResult = function (result) {
230
231
				if (result.status < 1) {
232
					OCA.notification.onFail(
233
						'Issue while retreiving the list of the Circles: ' +
234
						((result.error) ? result.error : 'no error message'));
235
					return;
236
				}
237
238
				divNavigation.children().each(function () {
239
					if ($(this).attr('id') != 'circles_search') {
240
						$(this).remove();
241
					}
242
				});
243
244
				var data = result.data;
245
				for (var i = 0; i < data.length; i++) {
246
247
					//	var curr = self.getCurrentCircleTemplate(data[i].id);
248
249
					var tmpl = $('#tmpl_circle').html();
250
251
					tmpl = tmpl.replace(/%title%/, data[i].name);
252
					tmpl = tmpl.replace(/%type%/, data[i].type);
253
					tmpl = tmpl.replace(/%owner%/, data[i].owner.userid);
254
					tmpl = tmpl.replace(/%status%/, data[i].user.status);
255
					tmpl = tmpl.replace(/%level_string%/, data[i].user.level_string);
256
					tmpl = tmpl.replace(/%count%/, data[i].count);
257
					tmpl = tmpl.replace(/%creation%/, data[i].creation);
258
259
					//	if (curr == null) {
260
					divNavigation.append(
261
						'<div class="circle" circle-id="' + data[i].id + '">' + tmpl + '</div>');
262
					//	} else {
263
					//		$(curr).html(tmpl);
264
					//	}
265
				}
266
267
				divNavigation.children('.circle').on('click', function () {
268
					self.selectCircle($(this).attr('circle-id'));
269
				});
270
			};
271
272
273
			this.selectCircle = function (circleid) {
274
				lastSearchUser = '';
275
				$('#addmember').val('');
276
277
				api.detailsCircle(circleid, this.selectCircleResult);
278
			};
279
280
281
			this.selectCircleResult = function (result) {
282
283
				$('#mainui #memberslist .table').children('tr').each(function () {
284
					if ($(this).attr('class') != 'header') {
285
						$(this).remove();
286
					}
287
				});
288
289
				if (result.status < 1) {
290
					OCA.notification.onFail(
291
						'Issue while retreiving the details of a circle: ' +
292
						((result.error) ? result.error : 'no error message'));
293
					return;
294
				}
295
296
				divNavigation.children('.circle').each(function () {
297
					if ($(this).attr('circle-id') == result.circle_id) {
298
						$(this).addClass('selected');
299
					} else {
300
						$(this).removeClass('selected');
301
					}
302
				});
303
				$('#emptycontent').hide(800);
304
				$('#mainui').fadeIn(800);
305
				currentCircle = result.circle_id;
306
				currentCircleLevel = result.details.user.level;
307
308
				if (result.details.user.level < 6) {
309
					$('#addmember').hide();
310
				} else {
311
					$('#addmember').show();
312
				}
313
314
				$('#joincircle_acceptinvit').hide();
315
				$('#joincircle_rejectinvit').hide();
316
				$('#joincircle_request').hide();
317
				$('#joincircle_invit').hide();
318
319
				if (result.details.user.level == 9) {
320
					$('#joincircle').hide();
321
					$('#leavecircle').hide();
322
				}
323
				else if (result.details.user.level >= 1) {
324
					$('#joincircle').hide();
325
					$('#leavecircle').show();
326
				} else {
327
					if (result.details.user.status == 'Invited') {
328
						$('#joincircle_invit').show();
329
						$('#joincircle_acceptinvit').show();
330
						$('#joincircle_rejectinvit').show();
331
						$('#joincircle').hide();
332
						$('#leavecircle').hide();
333
					}
334
					else if (result.details.user.status == 'Requesting') {
335
						$('#joincircle_request').show();
336
						$('#joincircle').hide();
337
						$('#leavecircle').show();
338
					}
339
					else {
340
						$('#joincircle').show();
341
						$('#leavecircle').hide();
342
					}
343
				}
344
345
				self.displayMembers(result.details.members);
346
			};
347
348
349
			this.searchMembersResult = function (response) {
350
351
				if (response === null ||
352
					(response.ocs.data.users === 0 && response.ocs.data.exact.users === 0)) {
353
					$('#members_search_result').fadeOut(300);
354
				}
355
				else {
356
					var currSearch = $('#addmember').val().trim();
357
					$('#members_search_result').children().remove();
358
359
					$.each(response.ocs.data.exact.users, function (index, value) {
360
						$('#members_search_result').append(
361
							'<div class="members_search exact" searchresult="' +
362
							value.value.shareWith + '">' + value.label + '   (' +
363
							value.value.shareWith + ')</div>');
364
					});
365
366
					$.each(response.ocs.data.users, function (index, value) {
367
						var line = value.label + '   (' + value.value.shareWith + ')';
368
						if (currSearch.length > 0) {
369
							line =
370
								line.replace(new RegExp('(' + currSearch + ')', 'gi'), '<b>$1</b>');
371
						}
372
373
						$('#members_search_result').append(
374
							'<div class="members_search" searchresult="' + value.value.shareWith +
375
							'">' + line + '</div>');
376
					});
377
378
					$('#members_search_result').children().first().css('border-top-width', '0px');
379
380
					$('.members_search').on('click', function () {
381
						api.addMember(currentCircle, $(this).attr('searchresult'),
382
							self.addMemberResult);
383
					});
384
					$('#members_search_result').fadeIn(300);
385
				}
386
387
			};
388
389
390
			this.addMemberResult = function (result) {
391
392
				if (result.status == 1) {
393
					OCA.notification.onSuccess(
394
						"Member '" + result.name + "' successfully added to the circle");
395
396
					self.displayMembers(result.members);
397
				}
398
				else {
399
					OCA.notification.onFail(
400
						"Member '" + result.name + "' NOT added to the circle: " +
401
						((result.error) ? result.error : 'no error message'));
402
				}
403
			};
404
405
406
			this.displayMembers = function (members) {
407
408
				$('#mainui #memberslist .table').children('tr').each(function () {
409
					if ($(this).attr('class') != 'header') {
410
						$(this).remove();
411
					}
412
				});
413
414
				if (members === null) {
415
					$('#mainui #memberslist .table').hide(200);
416
					return;
417
				}
418
419
				$('#mainui #memberslist .table').show(200);
420
				for (var i = 0; i < members.length; i++) {
421
422
					var tmpl = $('#tmpl_member').html();
423
424
					tmpl = tmpl.replace(/%username%/g, members[i].userid);
425
					tmpl = tmpl.replace(/%level%/g, members[i].level);
426
					tmpl = tmpl.replace(/%levelstring%/g, members[i].level_string);
427
					tmpl = tmpl.replace(/%status%/, members[i].status);
428
					tmpl = tmpl.replace(/%joined%/, members[i].joined);
429
					tmpl = tmpl.replace(/%note%/,
430
						((members[i].note) ? members[i].note : ''));
431
432
					$('#mainui #memberslist .table').append(tmpl);
433
				}
434
435
				$('#mainui #memberslist .table').children().each(function () {
436
					if ($(this).attr('member-level') == '9' || currentCircleLevel < 6) {
437
						$(this).children('.delete').hide(0);
438
					}
439
				});
440
441
				$('#mainui #memberslist .table .delete').on('click', function () {
442
					var member = $(this).parent().attr('member-id');
443
					api.removeMember(currentCircle, member, self.removeMemberResult);
444
				});
445
			};
446
447
448
			this.removeMemberResult = function (result) {
449
				if (result.status == 1) {
450
451
					$('#mainui #memberslist .table').children().each(function () {
452
						if ($(this).attr('member-id') == result.name) {
453
							$(this).hide(300);
454
						}
455
					});
456
457
					OCA.notification.onSuccess(
458
						"Member '" + result.name + "' successfully removed from the circle");
459
				}
460
				else {
461
					OCA.notification.onFail(
462
						"Member '" + result.name + "' NOT removed from the circle: " +
463
						((result.error) ? result.error : 'no error message'));
464
				}
465
466
			};
467
468
469
			this.joinCircleResult = function (result) {
470
				if (result.status == 1) {
471
472
					$('#mainui #memberslist .table').children().each(function () {
473
						if ($(this).attr('member-id') == result.name) {
474
							$(this).hide(300);
475
						}
476
					});
477
478
					if (result.member.level == 1) {
479
						OCA.notification.onSuccess(
480
							"You have successfully joined this circle");
481
					} else {
482
						OCA.notification.onSuccess(
483
							"You have requested an invitation to join this circle");
484
					}
485
					self.selectCircle(result.circle_id);
486
487
				}
488
				else {
489
					OCA.notification.onFail(
490
						"Cannot join this circle: " +
491
						((result.error) ? result.error : 'no error message'));
492
				}
493
			};
494
495
			this.leaveCircleResult = function (result) {
496
				if (result.status == 1) {
497
498
					$('#mainui #memberslist .table').children().each(function () {
499
						if ($(this).attr('member-id') == result.name) {
500
							$(this).hide(300);
501
						}
502
					});
503
504
					OCA.notification.onSuccess(
505
						"You have successfully left this circle");
506
507
					self.selectCircle(result.circle_id);
508
				}
509
				else {
510
					OCA.notification.onFail(
511
						"Cannot leave this circle: " +
512
						((result.error) ? result.error : 'no error message'));
513
				}
514
			};
515
516
			// getCurrentCircleTemplate: function (id) {
517
			//
518
			// 	currdiv = null;
519
			// 	$('#app-navigation.circles').children().each(function () {
520
			// 		if ($(this).attr('circle-id') == id) {
521
			// 			currdiv = $(this);
522
			// 			return false;
523
			// 		}
524
			// 	});
525
			// 	return currdiv;
526
			// }
527
		}
528
	};
529
530
531
	/**
532
	 * @constructs Notification
533
	 */
534
	var Notification = function () {
535
		this.initialize();
536
	};
537
538
	Notification.prototype = {
539
540
		initialize: function () {
541
542
			//noinspection SpellCheckingInspection
543
			var notyf = new Notyf({
544
				delay: 5000
545
			});
546
547
			this.onSuccess = function (text) {
548
				notyf.confirm(text);
549
			};
550
551
			this.onFail = function (text) {
552
				notyf.alert(text);
553
			};
554
555
		}
556
557
	};
558
559
	OCA.Circles.Navigation = Navigation;
560
	OCA.Circles.navigation = new Navigation();
561
562
	OCA.Notification = Notification;
563
	OCA.notification = new Notification();
564
565
});
566
567